﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Diagnostics;

using System.Diagnostics.Contracts;
using System.IO;

using System.Text.RegularExpressions;
using System.Configuration;
using System.Xml;
using ODataTestFrameworkCore.shared;
using ODataTestLauncherConsole;
using ODataRegression.Framework.Core.Shared;


namespace OODataTestLauncherConsole
{
    public class Program
    {

        static void Main(string[] args)
        {
            string testScriptName = ConfigurationSettings.AppSettings["TestConfigFileName"];

            if (args.Length > 0)
            {
                testScriptName = @"TestConfigs\" + args[0];

            }

            string outputXMLFileName =Path.Combine(Environment.CurrentDirectory, ProgramHelper.GetOutXMLFileName(testScriptName));
            string outputExcelFileName = outputXMLFileName.Replace(".xml",".xlsx");
            


            //Start Test Engine and execute test
            TestEngine testEngine = new TestEngine(testScriptName);
            TimeSpan executionTime = testEngine.ExecuteTest();
            testEngine.SaveResultAsXML(outputXMLFileName);

           
            //First try rendering excel 
            bool CreateExcelSuccess = ProgramHelper.CreateExcelReport(outputXMLFileName, outputExcelFileName);
           

            string OutPutExcelFileName = null;
            string logFileDropLocation = null;
            if (TestConfigReader.TestResultSettings.ContainsKey(TestResultSettingsTypes.LogFileDropLocation))
            {
                logFileDropLocation = TestConfigReader.TestResultSettings[TestResultSettingsTypes.LogFileDropLocation];
            }
           
            if (!string.IsNullOrEmpty(logFileDropLocation))
            {
                FileInfo fn = new FileInfo(outputXMLFileName);
                OutPutExcelFileName = outputXMLFileName + ".xlsx";

                File.Move(outputXMLFileName, Path.Combine(logFileDropLocation, fn.Name));
                if (CreateExcelSuccess)
                {
                    OutPutExcelFileName = Path.Combine(logFileDropLocation, fn.Name + ".xlsx");
                    File.Move(outputXMLFileName + ".xlsx", OutPutExcelFileName);
                }
            }
            else
            {
                if (CreateExcelSuccess)
                {
                   // System.Diagnostics.Process.Start(outputFileName + ".xlsx");
                    
                }
            }


            if (OutPutExcelFileName != null)
            {
                if (TestConfigReader.TestResultSettings.ContainsKey(TestResultSettingsTypes.EmailSubscriptionAliases))
                {
                    ProgramHelper.RaiseEmailAlert(OutPutExcelFileName, TestConfigReader.TestResultSettings[TestResultSettingsTypes.EmailSubscriptionAliases]);    
                }
                
            }


        }




    }
}
